// 20100225 sonofdelphi based new class on Edit In Place
/*
 * Edit In Place
 * http://josephscott.org/code/js/eip/
 *
 * Version: 0.2.2
 * License: http://josephscott.org/code/js/eip/license.txt
 */

if (Object.isUndefined(Proto)) { var Proto = { } }

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

Proto.EIP = Class.create({

	initialize:function(){
		this.options = Object.extend({
			item:			null,
			id:			false,
			editing:		'eip_editing',
			empty:			'eip_empty',
			type:			'text',
			is_empty:		false,
			empty_text:		'Fill me up!',
			orig_text:		false,
			options:		{ },
			cols:			0,
			postSave:		null,
			postCancel:		null,
                        preEdit:                null,
                        autoselect:             false
		}, arguments[0] || { });
	
		if(this.options.item){
			this.options.id= this.options.item.identify();
		}
		
		if($(this.options.id).innerHTML == '') {
			this.options.is_empty = true;
			$(this.options.id).innerHTML = '<span class="' + this.options.empty
				+ '">' + this.options.empty_text + '</span>';
		}
                else{
                    $(this.options.id).innerHTML = $(this.options.id).innerHTML.trim();
                }


		this.lookOut();
                this.options.item.addClassName('eip');
                Object.extend(this.options.item, this);
	},
	
	lookOut : function() {
		Event.observe(
			$(this.options.id),
			'dblclick',
			this.edit.bind(this),
			false
		);
	},
	
	formField: function() {
		var field = '';
	
		if(this.options.type == 'text') {
			var size = this.options.orig_text.length + 10;
			if(size >= 100) { size = 100; }
			if(this.options.size) { size = this.options.size; }
	
			field = '<span id="' + this.options.id + '_editor"><input id="'
				+ this.options.id + '_edit" class="' + this.options.editing 
                                + '" type="text" size="' + size
				+ '" value="' + this.options.orig_text + '" /><br />';
		}
		else if(this.options.type == 'textarea') {
			var cols = 25;
			if(this.options.cols) { cols = this.options.cols; }
			var rows = (this.options.orig_text.length / cols) + 3;
			if(this.options.rows) { rows = this.options.rows; }
	
			field = '<span id="' + this.options.id + '_editor"><textarea id="'
				+ this.options.id + '_edit" class="' + this.options.editing + '" rows="'
                                + rows + '" cols="'
				+ cols + '">' + this.options.orig_text + '</textarea><br />';
		}
		else if(this.options.type == 'select') {
			field = '<span id="' + this.options.id + '_editor"><select id="'
				+ this.options.id + '_edit" class="' + this.options.editing + '"';
	
			for(var i in this.options.options) {
				field += '<option id="' + this.options.id + '_option_' + i 
					+ '" value="' + i + '"';
				if(this.options.options[i] == this.options.id.innerHTML) {
					field += ' selected="selected"';
				}
				field += '>' + this.options.options[i] + '</option>';
			}
	
			field += '</select>&nbsp;';
		}
	
		return(field);
	},
	
	formButtons : function() {
		return(
			'<span><input id="' + this.options.id + '_save"  type="button" value="OK" /> '
			+ '<input id="' + this.options.id + '_cancel" '
                        + 'type="button" value="CANCEL" /> '
			+ '</span></span>'
		);
	},
	
	saveChanges : function() {
		var target = $(this.options.id);
		this.options.is_empty = false;

		var new_text = $F($(this.options.id + '_edit')).replace(/&/g, "&amp;").replace(/</g,"&lt;").replace(/>/g, "&gt;");

		if(new_text.blank()) {
			this.options.is_empty = true;
			new_text = this.options.empty_text;
		}
		Element.remove($(this.options.id + '_editor'));

		target.removeClassName(this.options.editing);
		target.innerHTML = new_text;
		target.show();

                if(this.options.postSave){
                    if((new_text!=this.options.orig_text) && (!this.options.is_empty))
                        var output = this.options.postSave(new_text, this.options.orig_text);
                        if(output)
                            target.innerHTML = output; //Repeating if post-save changes it again
                }
                
	},
	
	cancelChanges : function() {
		var target = $(this.options.id);

		Element.remove($(this.options.id + '_editor'));
		Element.removeClassName($(this.options.id), this.options.editing);

		if($(this.options.id).innerHTML == '') {
			this.options.is_empty = true;
			$(this.options.id).innerHTML = '<span class="' + this.options.empty
				+ '">' + this.options.empty_text + '</span>';
		}

		target.removeClassName(this.options.editing);
		target.show();
                if(this.options.postCancel){
                    this.options.postCancel();
                }
	},

	handlerEditKeypress : function(evt) {

            switch(evt.keyCode){
                case Event.KEY_RETURN:
                    this.saveChanges();
                    break;
                    
                case Event.KEY_ESC:
                    this.cancelChanges();
                    break;

                default:
                    return;
            }
 	},

	edit : function() {
		this.options.orig_text = $(this.options.id).innerHTML;
                if(this.options.preEdit)
                    this.options.orig_text = this.options.preEdit(this.options.orig_text);

                if(this.options.item.hasClassName(this.options.editing)){
                    $(this.options.id + '_edit').focus();
                    return;
                }
                
		Element.hide($(this.options.id));
		$(this.options.id).addClassName(this.options.editing);

		var field = this.formField();
		var button = this.formButtons();

		new Insertion.After($(this.options.id), field + button);
		if(this.options.is_empty)
			$(this.options.id + '_edit').value = '';
		Field.focus($(this.options.id + '_edit'));

                if(this.options.autoselect)
                    $(this.options.id + '_edit').select();

                Event.observe(
			$(this.options.id + '_save'),
			'click',
			this.saveChanges.bind(this),
			false
		);
		Event.observe(
			$(this.options.id + '_cancel'),
			'click',
			this.cancelChanges.bind(this),
			false
		);

		shortcut.add("Enter",this.handlerEditKeypress.bind(this),
                {'type':'keydown','propagate':false,'target':$(this.options.id + '_edit')});
		shortcut.add("Escape",this.handlerEditKeypress.bind(this),
                {'type':'keydown','propagate':false,'target':$(this.options.id + '_edit')});

// Retaining in case we want to get rid of shortcuts library dependency
//              $(this.options.id + '_edit').observe('keypress', this.handlerEditKeypress.bind(this));
	}
});

